<!-- 用户信息页面 - 只显示当前登录用户的信息 -->
<template>
  <div class="user-list-container">
    <h2>我的账户信息</h2>
    
    <!-- 加载状态 -->
    <div v-if="loading" class="loading">
      <p>加载中...</p>
    </div>
    
    <!-- 错误状态 -->
    <div v-else-if="error" class="error">
      <p>{{ error }}</p>
      <button @click="fetchCurrentUser" class="retry-btn">重试</button>
    </div>
    
    <!-- 当前用户信息 -->
    <div v-else-if="currentUser" class="user-card">
      <div class="user-avatar">
        {{ getUserInitial(currentUser.username) }}
      </div>
      <div class="user-info">
        <h3>{{ currentUser.username }}</h3>
        <p class="user-email">{{ currentUser.email || '无邮箱' }}</p>
        <p class="user-id">ID: {{ currentUser.id }}</p>
      </div>
    </div>
    
    <!-- 空状态 -->
    <div v-else class="empty-state">
      <p>无法加载用户数据</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import request from '../../utls/require'

const currentUser = ref<any>(null)
const loading = ref(false)
const error = ref('')

// 从localStorage获取当前登录用户ID
const getCurrentUserId = (): string => {
  try {
    const userStr = localStorage.getItem('user')
    if (userStr) {
      const user = JSON.parse(userStr)
      return user.id?.toString() || ''
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
  return ''
}

// 获取当前用户数据
const fetchCurrentUser = async () => {
  // 验证用户是否已登录
  const userId = getCurrentUserId()
  if (!userId) {
    error.value = '用户未登录，请先登录'
    return
  }
  
  loading.value = true
  error.value = ''
  
  try {
    // 调用获取当前用户信息的API
    const response = await request.get('/auth/user/')
    
    if (response && response.success) {
      currentUser.value = response.data
    } else {
      error.value = response?.message || '获取用户信息失败'
    }
  } catch (err: any) {
    error.value = err.message || '网络错误，请稍后重试'
  } finally {
    loading.value = false
  }
}

// 获取用户名首字母作为头像显示
const getUserInitial = (username: string): string => {
  return username ? username.charAt(0).toUpperCase() : '?'
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCurrentUser()
})
</script>

<style scoped>
.user-list-container {
  padding: 20px;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.user-list-container h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
}

/* 加载和错误状态 */
.loading, .error, .empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #666;
}

.error {
  color: #ff4d4f;
}

.retry-btn {
  margin-top: 10px;
  padding: 6px 16px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.retry-btn:hover {
  background-color: #40a9ff;
}

/* 用户卡片网格 */
.users-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.user-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.user-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: bold;
}

.user-info {
  flex: 1;
}

.user-info h3 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 18px;
}

.user-email {
  margin: 0 0 4px 0;
  color: #666;
  font-size: 14px;
}

.user-id {
  margin: 0;
  color: #999;
  font-size: 12px;
}
</style>